package com.juzipi.demo.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import java.util.Collection;
import java.util.List;


@Data
@TableName(value = "user")
//实现权限接口 UserDetails
public class User implements UserDetails {


    @TableId(type = IdType.AUTO)

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;
    //扩展字段
    @TableField(exist = false)
    private List<Role> roleList;
    @TableField(exist = false)
    private String token;

    //试试单例模式静态内部类

    //构造器私有化
    private User(){

    }


    //写一个静态内部类，该类中有一个静态的属性 UserInstance
    private static class UserInstance{
        private static final User INSTANCE = new User();
    }


    //提供一个静态的公有方法，直接返回UserInstance.INSTANCE
    public static User getInstance(){
        return UserInstance.INSTANCE;
    }

    //获取用户的角色列表
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roleList;
    }

    @Override
    public String getPassword() {
        return password;
    }


    @Override
    public String getUsername() {
        return username;
    }


    //帐户未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }


    //账户未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }


    //凭证未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


    //是否启用
    @Override
    public boolean isEnabled() {
        return true;
    }
}
